from pyExcelerator import Workbook
from pydispatch import dispatcher
from mogPageProcess import PageProcess
from mogPageProcess import TagParseThread
import re
import wx

class Model:

    def __init__(self):
        self.category = 'classical'
        self.pageStart = 1
        self.pageEnd = None
        self.startId = 1
        self.ringtongInfos = []
        self.pattern = re.compile('-+')
        self.state = 0
        self.currentPage = 0
        self.currentProcess = PageProcess(self.category, self.currentPage, self.ringtongInfos, self)
        self.tagParseThread = None
        self.tagParsed = False
        dispatcher.connect(self.onTagsParseDone, 'MSG_TAG_PARSE_COMPLITE')



    def clear(self):
        self.state = 0
        self.currentPage = 0
        self.tagParseThread = None
        self.tagParsed = False
        while len(self.ringtongInfos) > 0:
            self.ringtongInfos.pop()

        wx.CallAfter(dispatcher.send,'MSG_RESET_GRID_VIEW')



    def nextStep(self):
        if self.getNextPage() != None:
            self.nextPageExecute(self.getNextPage())
        elif self.tagParsed == False:
            self.parseTags()



    def onTagsParseDone(self):
        self.tagParsed = True



    def nextPageExecute(self, nextPage):
        self.currentPage = nextPage
        self.currentProcess.reset(self.category, self.currentPage, self.startId)
        self.currentProcess.execute()



    def getEndPage(self):
        if self.pageEnd == None:
            return self.pageStart
        else:
            return self.pageEnd



    def isDone(self):
        return self.tagParsed == True



    def execute(self):
        if self.currentPage == 0:
            self.currentPage = self.pageStart
            self.currentProcess.reset(self.category, self.currentPage, self.startId)
            self.currentProcess.execute()
        elif self.currentPage != 0 and self.currentProcess.isDone() == False:
            self.currentProcess.execute()
        else:
            self.nextStep()



    def parseTags(self):
        self.tagParseThread = TagParseThread(self.ringtongInfos)
        self.tagParseThread.setDaemon(True)
        self.tagParseThread.start()



    def getNextPage(self):
        if self.currentPage == self.getEndPage() or self.currentPage > self.getEndPage():
            return None
        else:
            return self.currentPage + 1



    def stopThreads(self):
        if self.currentProcess != None:
            self.currentProcess.appExit = True
        if self.tagParseThread != None:
            self.tagParseThread.appExit = True



    def export(self):
        wb = Workbook()
        ws = wb.add_sheet(self.category)
        i = 0
        for item in self.ringtongInfos:
            ws.write(i, 0, item['displayId'])
            ws.write(i, 1, item['title'])
            ws.write(i, 2, item['filename'])
            ws.write(i, 3, item['size'])
            ws.write(i, 4, item['format'])
            tags = ''
            for x in item['tags']:
                tags += x + ','

            ws.write(i, 5, tags.rstrip(','))
            i += 1

        filename = self.category + '-' + str(self.pageStart)
        if self.pageEnd != None:
            filename = filename + '-' + str(self.pageEnd)
        filename += '-list.xls'
        wb.save(filename)
        return filename



if __name__ == '__main__':
    m = Model()
    m.pageEnd = 2
    m.startProcess()

